﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StaticData.cs" company="">
//   
// </copyright>
// <summary>
//   The static data.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Linq;

namespace Systemathics.FrameWork
{
    public sealed class StaticData
    {
        public string Name              { get; private set; }
        public Guid  Id                 { get; private set; }
        public string Index             { get; private set; }
        public double? Capitalization   { get; private set; }
        public AssetClass? Classe       { get; private set; }
        public Sectors Sector           { get; private set; }
        public InstrumentType Type      { get; private set; }
        public string Country           { get; private set; }
        public string ISIN              { get; private set; }
        public double Refinancing       { get; private set; }
        public DateTime? Maturity       { get; private set; }
        public Exchange Exchange        { get; private set; }
        public Currency Currency        { get; private set; }
        public Rule Rules               { get; private set; }
        public Fees Fees                { get; set; }

        private DBInstruments dbi;

        public StaticData(DBInstruments dbInstruments)
        {
            Fill(dbInstruments);
        }
        public StaticData(Guid InsrumentId)
        {
            using (var db = new SystemathicsEntities(GlobalVariables.EntitiesConnectionString()))
                Fill(db.DBInstruments.Where(u => u.ID == InsrumentId).FirstOrDefault());
        }

        private void Fill(DBInstruments dbInstrument)
        {
            dbi = dbInstrument;
            Id = dbi.ID;
            Name = dbi.Name;
            ISIN = dbi.ISIN;
            Maturity = dbi.Maturity;
            Country = dbi.Country;
            Sector = (Sectors)Enum.Parse(typeof(Sectors), dbi.Sector, true);
            Classe = (AssetClass)Enum.Parse(typeof(AssetClass), dbi.Classe, true);
            Type = (InstrumentType)Enum.Parse(typeof(InstrumentType), dbi.Type, true);
            Capitalization = dbi.Capitalization;
            Index = dbi.BelongToIndex;
        }

        public void LoadExchange()
        {
            if (!dbi.DBExchangeReference.IsLoaded)
                dbi.DBExchangeReference.Load();
            Exchange = new Exchange(dbi.DBExchange.Exchange, dbi.DBExchange.ExchangeName, dbi.DBExchange.Country, dbi.DBExchange.OpenAuctions, dbi.DBExchange.Openning, dbi.DBExchange.CloseAuctions, dbi.DBExchange.Closing);
        }
        public void LoadRules()
        { 
            if (!dbi.DBRulesReference.IsLoaded)
                dbi.DBRulesReference.Load();
            Rules = new Rule(dbi.ID, dbi.DBRules.Tick, dbi.DBRules.TickValue, dbi.DBRules.isBuyAllowed, dbi.DBRules.isSellAllowed, dbi.DBRules.isShortable, dbi.DBRules.isHardToBorrow, dbi.DBRules.MaxNominalOrder);
        }
        public void LoadCurrency()
        {
            if (!dbi.DBCurrencyReference.IsLoaded)
                dbi.DBCurrencyReference.Load();
            Currency = new Currency(dbi.DBCurrency.CurrencyName, dbi.DBCurrency.Ticker, (double)dbi.DBCurrency.ReferencePrice);
        }
    }
} 